<template>
  <div>
    <p-modal
      title="Title"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <p-form-model
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <p-form-model-item
          ref="exceptionType"
          label="异常类别 "
          prop="exceptionType"
        >
          <p-select v-model="form.exceptionType">
            <p-select-option v-for="(item, key) in hrTsExceptionTypeKeys" :key="key" :value="key">
              {{item}}
            </p-select-option>
          </p-select>     
        </p-form-model-item>

        <p-form-model-item
          ref="errorReasonBig"
          label="故障原因大类"
          prop="errorReasonBig"
        >
          <p-select v-model="form.errorReasonBig">
            <p-select-option v-for="(item, index) in badGroupTypeList" :key="index" :value="item.badGroupCode">
              {{item.badGroupName}}
            </p-select-option>
          </p-select> 

        </p-form-model-item>

        <p-form-model-item ref="unstandardWtcode" label="非标工时代码" prop="unstandardWtcode">
          <p-select v-model="form.unstandardWtcode">
            <p-select-option v-for="(item, key) in unstandardWtcodeKeys" :key="key" :value="key">
              {{item}}
            </p-select-option>
          </p-select>  

        </p-form-model-item>
      </p-form-model>
    </p-modal>
  </div>
</template>
<script>
import { postAction, getAction, putAction, deleteAction } from "@/api/manage";
import { getDictItemsArr } from "@/utils/util";

export default {
  data() {
    return {
      visible: false,
      confirmLoading: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 12 },
      state: "add",
      form: {
        errorReasonBig: "",
        exceptionType: "",
        unstandardWtcode: "",
      },
      rules: {
        errorReasonBig: [
          { required: true, message: "请输入故障原因大类", trigger: "blur" },
        ],
        exceptionType: [
          { required: true, message: "请输入异常类别", trigger: "blur" },
        ],
        unstandardWtcode: [
          { required: true, message: "请输入非标工时代码", trigger: "blur" },
        ],
      },
      //异常类别 数字字典
      hrTsExceptionTypeKeys: {},  
      //非标工时代码 数字字典
      unstandardWtcodeKeys: {},
      badGroupTypeList: [], //异常类别列表
      url: {
        add: "/mes-manpower/mesHrUnstandardWorktime",
        badGroupType: '/mes-quality/mesQcDefectProjectMaintain/selectByBadGroupType',  // 异常类别列表
        edit: "",
      },
    };
  },
  mounted() {
    this.getAjaxGetDictItems()
    this.getBadGroupTypeList()
  },
  methods: {

    // 获取数字字典
    getAjaxGetDictItems() {
      const arr = [
        { code: "mes_hr_us_exception_type", key: "hrTsExceptionTypeKeys" },
        { code: "mes_unstandard_wtcode", key: "unstandardWtcodeKeys" },
      ];
      getDictItemsArr(this, arr);
    },


    getBadGroupTypeList(){
      getAction(this.url.badGroupType  + "/" + "2").then(res => {
        if(res.success) {
          this.badGroupTypeList = res.data
        }
      })
    },

    showModal(record) {
      if (record) {
        this.state = "edit";
        for (let key in this.form) {
          this.form[key] = record[key];
        }
        this.editId = record.id;
      } else {
        this.state = "add";
      }

      this.visible = true;
    },
    handleOk(e) {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.form));
          this.confirmLoading = true;
          if (this.state == "add") {
            postAction(this.url.add, params).then((res) => {
              if (res.success) {
                this.visible = false;
                this.$emit("handleSuccess");
              } else {
                this.$message.error(res.msg);
              }
              this.confirmLoading = false;
            });
          }

          if (this.state == "edit") {
            params.id = this.editId;
            putAction(this.url.edit, params).then((res) => {
              if (res.success) {
                this.visible = false;
                this.$emit("handleSuccess");
              } else {
                this.$message.error(res.msg);
              }
              this.confirmLoading = false;
            });
          }
        }
      });
    },
    handleCancel(e) {
      this.visible = false;
      this.$refs.ruleForm.resetFields()
      Object.assign(this.form, this.$options.data().form);
    },
  },
};
</script>
